#delim cr
set more off
pause on
graph set ps logo off

capture log close
set linesize 200
set logtype text
log using clean-noaa-data.log , replace

/* --------------------------------------

AUTHOR: Tal Gross

PURPOSE: Clean the CSV files I got from NOAA
for two cities in China.

DATE CREATED: April 19, 2014

NOTES:

--------------------------------------- */

clear all
estimates clear
set mem 500m

************************************************************
**   Bring in CSV files
************************************************************

insheet using 324215.csv , comma names
d, f

tempfile t324215 
save `t324215'

insheet using 324335.csv , comma names clear

append using `t324215'


************************************************************
**   Clean up precipitation
************************************************************

sum prcp
rename prcp precipitation
replace precipitation = . if precipitation == -9999

************************************************************
**   Clean up temperature 
************************************************************

** The temperatures are in celsius, in tenths of a degree
foreach var in max min {
	sum t`var'
	replace t`var' = . if t`var' == -9999
	replace t`var' = t`var' / 10
	rename t`var' t`var'_cel
	gen t`var'_fah = (9 * t`var'_cel) / 5 + 32
}

codebook tmax_cel tmin_cel precipitation

************************************************************
**   Clean up date
************************************************************

sum date
list date in 1/10
gen year = floor(date/1e4)
gen month = floor((date - year * 1e4 ) / 1e2)
gen day = date - 1e4 * year - 1e2 * month
list date year month day in 1/10
rename date date_orig
gen date = mdy( month , day , year)
format date %td

codebook date

************************************************************
**   Sanity checks
************************************************************

** Just describe weather by month
table month , c(mean tmin_fah mean tmax_fah)

************************************************************
**   Polish off
************************************************************

compress
keep date station_name precipitation tmax_* tmin_* 
sort station_name date

save cleaned-shanghai-beijing-noaa.dta , replace


log close
exit

